<template>
  <div style="background:#ddd">
    <div v-if="equiinfolist.hash">
      <header class="mint-header" style="font-size: 18px;">
        <div class="mint-header-button is-left">
          <button class="mint-button mint-button--default mint-button--normal" style="font-size: 15px;" @click="option">
            <span class="mint-button-icon">
              <i class="mintui mintui-back"></i>
            </span>
            <label class="mint-button-text">返回</label>
          </button>
        </div>
        <h1 class="mint-header-title">详情页</h1>
        <div class="mint-header-button is-right"></div>
      </header>
      <!-- <div style="height:40px"/> -->
    </div>
    <div style="width:95%;margin:10px auto 0">
      <div v-if="equiinfolist.hash">
        <mt-cell :value="equiinfolist.hash.ep_ename" title="设备名称" />
        <mt-cell :value="equiinfolist.hash.ep_typename" title="分类" />
        <mt-cell :value="equiinfolist.hash.managenum" title="管理编号" />
        <mt-cell :value="equiinfolist.hash.orgname" title="所在部门" />
        <mt-cell title="使用人">
          <mt-button type="primary" size="small" @click="adduser">添加使用人</mt-button>
        </mt-cell>
        <div v-if="userList.length>0">
          <mt-cell v-for="(item, index) in userList" :key="index" :title="index+1+': '+item.hash.username" :label="'检测项目：'+item.hash.verifiy_project">
            <i class="mint-cell-allow-right" @click="edituser(item,index)" />
          </mt-cell>
        </div>
        <div v-else style="text-align:center;background:#fff;color:#aaa;padding:10px 0">暂无使用人</div>
        <mt-cell :value="equiinfolist.hash.scale" title="规格型号" />
        <mt-cell :value="equiinfolist.hash.range" title="测量范围" />
        <mt-cell :value="equiinfolist.hash.accuracy" title="精准度等级" />
        <mt-cell :value="equiinfolist.hash.factory_num" title="出厂编号" />
        <mt-cell :value="equiinfolist.location" title="放置地点" />
        <mt-cell :value="equiinfolist.bytime" title="购置日期" />
        <mt-cell :value="equiinfolist.hash.factory" title="生产厂家" />
        <mt-cell :value="equiinfolist.hash.traceability" title="溯源方式" />
        <mt-cell :value="equiinfolist.hash.verifiy_company" title="检定/校准单位" />
        <mt-cell :value="equiinfolist.hash.verifiytime" title="本次检定/校准日期" />
        <mt-cell :value="equiinfolist.hash.period_times" title="期间核查次数" />

        <mt-cell :value="equiinfolist.hash.chargername" title="负责人" />
        <mt-cell :value="equiinfolist.hash.remark" title="备注" />
        <mt-cell :value="equiinfolist.hash.statusname" title="状态" />
        <mt-cell title="设备照片">
          <span @click="popupVisible1=true">
            <img slot="icon" :src="equiinfolist.hash.equ_photo" width="24" height="24" />
          </span>
        </mt-cell>
      </div>
      <div v-if="fileurl">
        <mt-cell v-for="(item, index) in fileurl" :key="index" title="附件">
          <span>
            <a :href="item.hash.fileurl" download>{{ item.hash.filename }}</a>
          </span>
        </mt-cell>
      </div>
      <div>
        <mt-button type="primary" size="large" @click="emitshebei">保存</mt-button>
      </div>
    </div>
    <mt-popup v-model="popupVisible1" popup-transition="popup-fade">
      <div v-if="equiinfolist.hash" style="width:200px;">
        <img :src="equiinfolist.hash.equ_photo" alt style="width:100%" />
      </div>
    </mt-popup>
    <mt-popup v-model="shownewuser" position="right" style="height:100%;width:100%">
      <div>
        <div style="font-size:16px;font-weight:bold;text-align:center;line-height:40px">{{ newusertitle }}</div>
        <div class="select" @click="showchooseuser = true">
          <mt-cell title="选择使用人" is-link>
            <span>{{ currentTags?currentTags: '请选择' }}</span>
          </mt-cell>
        </div>
        <mt-field v-model="newsyrdata.hash.verifiy_project" label="检测项目" placeholder="请输入检测项目" />
        <mt-field v-model="newsyrdata.hash.measurement" label="量值" placeholder="请输入量值" />
        <div v-if="newusertitle=='添加使用人'" style="width:100%;text-align:center;margin:25px 0 15px 0;">
          <mt-button type="primary" style="font-size:16px;height:35px" @click="shownewuser=false">取消</mt-button>

          <mt-button type="danger" style="font-size:16px;height:35px" @click="submitadduser">添加</mt-button>
        </div>
        <div v-else style="width:100%;text-align:center;margin:25px 0 15px 0;">
          <mt-button type="primary" style="font-size:16px;height:35px" @click="shownewuser=false">取消</mt-button>
          <mt-button type="primary" style="font-size:16px;height:35px" @click="deleteuser">删除</mt-button>
          <mt-button type="danger" style="font-size:16px;height:35px" @click="submitedituser">保存</mt-button>
        </div>
      </div>
    </mt-popup>

    <mt-popup v-model="showchooseuser" position="bottom" class="mint-popup" style="width:100%">
      <mt-picker ref="picker" :slots="slots" :visible-item-count="5" :show-toolbar="true" value-key="label">
        <mt-button type="primary" style="width:100%;border-radius:0" size="small" @click="handleConfirm">确认</mt-button>
      </mt-picker>
    </mt-popup>

    <mt-popup v-model="popupVisible2" popup-transition="popup-fade">
      <div style="width:100%">
        <div style="font-size:16px;font-weight:bold;text-align:center;line-height:40px">编辑设备</div>
        <mt-field v-model="verifiy_param" label="检定参数" placeholder="请输入检定参数" type="email" />
        <div style="width:100%;text-align:center;margin:25px 0 15px 0;">
          <mt-button type="primary" style="font-size:16px;height:35px" @click="popupVisible2=false">取消</mt-button>
          <mt-button type="danger" style="font-size:16px;height:35px" @click="emitshebei">保存</mt-button>
        </div>
      </div>
    </mt-popup>
  </div>
</template>
<script>
export default {
  components: {},
  data () {
    return {
      verifiy_param: '',
      showchooseuser: false,
      popupVisible2: false,
      shownewuser: false,
      newusertitle: '添加使用人',
      popupVisible1: false,
      htitle: '详情页',
      username: '',
      ts: this.newStore(),
      equiinfolist: {},
      fileurl: [],
      userList: [],
      userindex: '',
      currentTags: '',
      jbrList: [],
      slots: [
        {
          flex: 1,
          values: [],
          textAlign: 'center'
        }
      ],
      newsyrdata: {
        hash: {
          username: '',
          pid: ''
        }
      }
    }
  },
  watch: {
    shownewuser (newVal, oldVal) {
      if (newVal == true) {
        let cssStr = 'overflow-y: hidden; height: 100%;'
        document.getElementsByTagName('html')[0].style.cssText = cssStr
        document.body.style.cssText = cssStr
      } else {
        let cssStr = 'overflow-y: auto; height: auto;'
        document.getElementsByTagName('html')[0].style.cssText = cssStr
        document.body.style.cssText = cssStr
      }

      // 下面需要这两行代码，兼容不同浏览器
      document.body.scrollTop = this.pageScrollYoffset
      window.scroll(0, this.pageScrollYoffset)
    }
  },
  created () {
    this.getjbrList()
    this.getonelist()
  },
  methods: {
    handleConfirm () {
      console.log(this.$refs.picker.getValues())

      this.currentTags = this.$refs.picker.getValues()[0].label
      this.newsyrdata.hash.pid = this.$refs.picker.getValues()[0].value
      this.newsyrdata.hash.username = this.$refs.picker.getValues()[0].label
      this.showchooseuser = false
    },
    edituser (item, index) {
      this.newusertitle = '编辑使用人'
      this.newsyrdata = item
      this.userindex = index
      this.currentTags = item.hash.username
      this.shownewuser = true
    },
    option () {
      this.$router.push({
        name: 'equipment',
        query: {
          name: ''
        }
      })
    },
    submitadduser () {
      this.userList.push(this.newsyrdata)
      this.shownewuser = false
    },
    submitedituser () {
      this.shownewuser = false
    },
    deleteuser () {
      this.userList.splice(this.userindex, 1)
      this.shownewuser = false
    },
    adduser () {
      this.newusertitle = '添加使用人'
      this.currentTags = ''
      this.newsyrdata = {
        hash: {
          username: '',
          pid: ''
        }
      }
      this.shownewuser = true
    },
    // 获取经办人列表
    getjbrList () {
      this.jbrList = []
      this.slots[0].values = []
      const req = {
        handle:
          '45EE8FEFE87870C53A0536D8566DA8C94345851CDDF60960CBCA139672EA5B8C230BE336D992C9E341DC33B3A5E90841',
        query: ''
      }
      this.spost(this.ts, '/mainservlet', req).then(ts => {
        this.jbrList = ts.d.data.operation1
        if (this.jbrList.length > 0) {
          for (let i = 0; i < this.jbrList.length; i += 1) {
            let a = {}
            a.label =
              this.jbrList[i].hash.username + '-' + this.jbrList[i].hash.orgname
            a.value = this.jbrList[i].hash.pid
            a.orgname = this.jbrList[i].hash.orgname
            this.slots[0].values.push(a)
          }
        }
      })
    },
    //获取单挑数据
    getonelist () {
      const req = {
        handle:
          'DD5D108B99FC12B72DC0B97F927BBE3DD7F2412F8A4ED57CBFF0B9210751F692',
        id: this.$route.query.id
      }
      this.spost(this.ts, '/mainservlet', req).then(ts => {
        this.equiinfolist = ts.d.data.operation1[0]
        this.userList = ts.d.data.operation2
        this.fileurl = ts.d.data.operation3
        console.log(this.equiinfolist)
      })
    },
    //保存检定
    emitshebei () {
      const req = {
        handle:
          'DD5D108B99FC12B72DC0B97F927BBE3D38BC3A756AD0A780BC8C24080344A196',
        id: this.$route.query.id,
        verifiy_param: this.verifiy_param,
        syrid: [],
        verifiy_project: [],
        measurement: []
      }
      for (let i = 0; i < this.userList.length; i += 1) {
        req.syrid.push(this.userList[i].hash.pid)
        req.verifiy_project.push(this.userList[i].hash.verifiy_project)
        req.measurement.push(this.userList[i].hash.measurement)
      }
      this.spost(this.ts, '/mainservlet', req).then(ts => {
        if (ts.d.sflag == 'success') {
          this.$messagebox('提示', '保存成功')
          this.popupVisible2 = false
          this.getonelist()
        }
      })
    }
  }
}
</script>
<style scoped>
.mint-cell-value input {
  text-align: right;
}
.mint-popup {
  width: 95%;
}
</style>
